package dr.joketalker.controller;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;


import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.Toast;
import dr.joketalker.R;
import dr.joketalker.controller.view.GridItem;
import dr.joketalker.controller.view.GridViewAdapter;
import dr.joketalker.dao.CategoryDAO;
import dr.joketalker.dao.LanguageDAO;
import dr.joketalker.entity.Joke;
import dr.joketalker.model.VoiceRecorderModel;
public class VoiceRecorderController extends MyController implements OnItemSelectedListener{

	CategoryDAO categoryDao = null;
	LanguageDAO languageDao = null;
		
	protected GridItem giClear;
	protected GridItem giUpload;
	
	protected GridItem giPlay;
	protected GridItem giRecord;
	protected GridItem giStop;
	
	protected GridView gdvControl;
    protected GridViewAdapter gdvAdapterControl = new GridViewAdapter(this);
    
	protected GridView gdvOption;
    protected GridViewAdapter gdvAdapterOption = new GridViewAdapter(this);
	
	protected EditText etTitle;
	protected EditText etDescription;
	
	protected Spinner spCategory;
	protected Spinner spLanguage;
	
	Chronometer chTime;
	
	protected VoiceRecorderModel mModel = new VoiceRecorderModel();
	
	 @Override
	public void onCreate(Bundle savedInstanceState) {
		 
		 
	        super.onCreate(savedInstanceState);
	        mModel.addObserver(this);
	
	        
	        setContentView(R.layout.voicerecorder);
	        
	        gdvControl = (GridView)findViewById(R.id.gdvControl);
	        
	        List<GridItem> listGridItem = new LinkedList<GridItem>();
	        giRecord = new GridItem(this,R.string.record,R.drawable.record);
	        listGridItem.add(giRecord);
	        giPlay = new GridItem(this,R.string.play,R.drawable.play);
	        listGridItem.add(giPlay);
	        
	        giStop = new GridItem(this,R.string.stop,R.drawable.stop);
	        listGridItem.add(giStop);
	        
	        for(GridItem gi : listGridItem){
	        	gdvAdapterControl.addItem(gi);
	        }

	        gdvControl.setAdapter(gdvAdapterControl);
	        gdvControl.setOnItemClickListener(this);
	        
	        
	        //==============================================================
	     
	        
	        gdvOption = (GridView)findViewById(R.id.gdvOption);
	        List<GridItem> listOptionGridItem = new LinkedList<GridItem>();
	        
	        giClear = new GridItem(this,R.string.clear,R.drawable.remove);
	        listOptionGridItem.add(giClear);
	        
	        giUpload = new GridItem(this,R.string.upload,R.drawable.upload);
	        listOptionGridItem.add(giUpload);
	        

	        
	        for(GridItem gi : listOptionGridItem){
	        	gdvAdapterOption.addItem(gi);
	        }

	        gdvOption.setAdapter(gdvAdapterOption);
	        gdvOption.setOnItemClickListener(this);
	        
	        //==============================================================

	        
	        etTitle 		= (EditText)findViewById(R.id.etTitle);
	        etDescription 	= (EditText)findViewById(R.id.etDescription);
	        
	        spCategory		= (Spinner)findViewById(R.id.spCategory);
	        spLanguage		= (Spinner)findViewById(R.id.spLanguage);
	        
	        chTime		= (Chronometer)findViewById(R.id.chTime);
	        

	        
	        spCategory.setOnItemSelectedListener(this);
	        spLanguage.setOnItemSelectedListener(this);


	        spLanguage.setSelection(14);
	        spCategory.setSelection(7);
	        
	        /*
	        readCategories();
	        readLanguages();
	         */
	        init();
	 }


	@Override
	public void onClick(View v) {

	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
		super.onItemClick(arg0, v, arg2, arg3);
		
		if(giRecord.equals(v)){
			mModel.startRecording("recordedJoke");
			chTime.setBase(SystemClock.elapsedRealtime());

			//chTime.start();
		}else if(giStop.equals(v)){
			mModel.stop();
			//chTime.stop();

			
		}else if(giPlay.equals(v)){
			mModel.startPlaying();
			chTime.setBase(SystemClock.elapsedRealtime());

			
		}else if(giClear.equals(v)){
			clear();
			
		}else if(giUpload.equals(v)){
			Joke joke = getJoke();
			upload(joke);
			
		}		
		
	}
	
	
	public Joke getJoke(){
		Joke joke = new Joke();
		
		joke.setTitle(etTitle.getText().toString());
		joke.setDescription(etDescription.getText().toString());
		joke.setLanguage(spLanguage.getSelectedItem().toString());
		joke.setCategory(spCategory.getSelectedItem().toString());
		
		return joke;
	}	
	
	public void upload(Joke joke){
		showFullLoading();
		mModel.upload(joke);
	}
	
	
	public void readCategories(){	

		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(VoiceRecorderController.this, R.id.spCategory, R.array.arrCategory);
		spCategory.setAdapter(adapter);
	}
	
	public void readLanguages(){
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(VoiceRecorderController.this, R.id.spLanguage, R.array.arrLanguage);
		spLanguage.setAdapter(adapter);
		
	}

	
	public void clear(){
		etTitle.setText("");
		etDescription.setText("");
	}

	
	public void showNewCategoryDialog(){
		
	}


	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void update(Observable observable, Object data) {
		super.update(observable, data);
		if(mModel.getMediaPlayer().isPlaying() || mModel.isRecording()){
			chTime.start();
		}else{
			chTime.stop();
		}
		
	}
	
}
